package com.lemner.www.hongyu.Adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.lemner.www.hongyu.Activity.DingdanActivity;
import com.lemner.www.hongyu.Activity.MainActivity;
import com.lemner.www.hongyu.Bean.RoborderBean;
import com.lemner.www.hongyu.Bean.UndistributedOrderbean;
import com.lemner.www.hongyu.R;
import com.lemner.www.hongyu.Service.Service;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by 小博 on 2017/10/31.
 */

public class UndistributedorderAdapter extends RecyclerView.Adapter {
    private UndistributedOrderbean.DataBean bean;
    private Context context;
    private LayoutInflater layoutInflater;
    private static String BASEURL="http://www.hongyudasha.com/propertysystem/";
    private Retrofit retrofit;
    private Service service;
    private int order_id,worker_id;
    private  String ids;


    public UndistributedorderAdapter(Context context) {
        this.context=context;
        layoutInflater = LayoutInflater.from(context);
    }

    public void setList(UndistributedOrderbean.DataBean bean) {
        this.bean = bean;
        notifyDataSetChanged();
    }
    public void setWorkerid(int workerid){
        this.worker_id=workerid;
    }
    public void  setIds(String id){
        this.ids=id;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_undistributed, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        UndistributedOrderbean.DataBean.ListBean date=bean.getList().get(position);
        order_id=bean.getList().get(position).getOrder_id();
        ViewHolder viewHolder= (ViewHolder) holder;
        if(date==null)
            return ;
      viewHolder.tv_cause.setText(date.getCause());
        viewHolder.tv_address.setText(date.getAddress());
       viewHolder.tv_ordernum.setText(date.getOrder_number());
        viewHolder.tv_jiedan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getDate();
            }
        });
    }

    @Override
    public int getItemCount() {

        if(bean==null)
            return 0;
            return bean.getList().size();
    }
    public  class  ViewHolder extends  RecyclerView.ViewHolder {
        private TextView tv_ordernum,tv_address,tv_cause,tv_jiedan;
        public ViewHolder(View itemView) {
            super(itemView);
            tv_ordernum=(TextView)itemView.findViewById(R.id.tv_ordernum);
            tv_address=(TextView)itemView.findViewById(R.id.tv_address);
            tv_cause=(TextView)itemView.findViewById(R.id.tv_cause);
            tv_jiedan=(TextView)itemView.findViewById(R.id.tv_jiedan);
        }

    }
    //接单
    public void getDate(){
        retrofit=new Retrofit.Builder().baseUrl(BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        Service service=retrofit.create(Service.class);
        Call<RoborderBean>call=service.getJiedan(order_id,worker_id);
        call.enqueue(new Callback<RoborderBean>() {
            @Override
            public void onResponse(Call<RoborderBean> call, Response<RoborderBean> response) {
                    Toast.makeText(context, "接单成功", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(context, MainActivity.class);
                    context.startActivity(intent);
            }

            @Override
            public void onFailure(Call<RoborderBean> call, Throwable t) {
                Toast.makeText(context,"失败",Toast.LENGTH_SHORT).show();
            }
        });

    }

}
